package com.ql.dao;

import com.ql.domain.Menu;
import com.ql.domain.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface IUserDao {
    /**
     * 根据userName查询用户
     *
     * @param username
     * @return
     * @throws Exception
     */
    @Select("select * from users where username=#{username}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            @Result(property = "name", column = "name"),
            @Result(property = "systemID", column = "systemID"),
            @Result(property = "userLevel", column = "userLevel"),
            @Result(property = "tel", column = "tel"),
            @Result(property = "state", column = "state"),
            @Result(property = "gender", column = "gender"),
            @Result(property = "permissions", column = "id", javaType = java.util.List.class, many = @Many(select = "com.ql.dao.PermissionDao.findByUserId")),
            @Result(property = "menus", column = "id", javaType = java.util.List.class, many = @Many(select = "com.ql.dao.MenuDao.selectListByUserId")),
            @Result(property = "sections", column = "id", javaType = java.util.List.class, many = @Many(select = "com.ql.dao.SectionDao.findByUserId")),
            @Result(property = "selectTemplates", column = "id", javaType = java.util.List.class, many = @Many(select = "com.ql.dao.SelectTemplateDao.findByUserId"))
    })
    UserInfo findByUsername(String username) throws Exception;

    /**
     * 根据name查询用户
     *
     * @param name
     * @return
     */
    List<UserInfo> findAll(String name);

    /**
     * 保存用户
     *
     * @param userInfo
     * @return
     */
    int save(UserInfo userInfo);

    /**
     * 添加初始权限
     *
     * @param userId
     * @return
     */
    @Insert("insert into users_permission values(#{userId},#{permissionId})")
    int saveInitUser(@Param("userId") Integer userId, @Param("permissionId") Integer permissionId);

    @Insert("insert into users_menu values(#{userId},#{menuId})")
    void saveInitMenu(@Param("userId") Integer userId, @Param("menuId") Integer menuId);

    /**
     * 更新用户
     *
     * @param userInfo
     * @return
     */
    void update(UserInfo userInfo);


    @Update("update users_permission set permissionId=#{userLevel} where userId=#{userId}")
    void updateUserLevel(@Param("userLevel") Integer userLevel,@Param("userId")Integer userId);


    /**
     * 根据userId删除关联的导航栏
     *
     * @param id
     */
    @Delete("delete from users_menu where userId=#{id}")
    void delMenuByUserId(Integer id);

    /**
     * 根据userId删除关联的权限
     *
     * @param id
     */
    @Delete("delete from users_permission where userId=#{id}")
    void delPermissionByUserId(Integer id);

    /**
     * 根据id删除用户
     *
     * @param id
     */
    @Delete("delete from users where id=#{id}")
    void delUser(Integer id);

    /*   */

    /**
     * 根据userId删除所有菜单权限
     *
     * @param userId
     * @return
     */
    @Delete("delete from users_menu where userId=#{userId}")
    void deleteMenuByUserId(Integer userId);

    /**
     * 根据userId添加菜单权限
     *
     * @param userId
     * @param menuId
     */
    @Insert("insert into users_menu values(#{userId},#{menuId})")
    void addUserMenu(@Param("userId") Integer userId, @Param("menuId") Integer menuId);

    /**
     * 根据userId获取对应的权限id列表用于展示
     *
     * @param userId
     * @return
     */
    @Select("select permissionId from users_permission where userId =#{userId}")
    List<Integer> selectPermissionIdByUserId(String userId);

    /**
     * 根据userId添加操作权限
     *
     * @param userId
     * @param PermissionId
     */
    @Insert("insert into users_permission values(#{userId},#{PermissionId})")
    void addPermissionUser(@Param("userId") String userId, @Param("PermissionId") String PermissionId);


    /**
     * 根据userId查询用户
     *
     * @param userId
     * @return
     */
    @Select("select * from users where id=#{userId}")
    UserInfo findByUserId(String userId);

    /**
     * 根据userId删除中间表数据
     *
     * @param userId
     */
    @Delete("delete from users_section where userId=#{userId}")
    void delUserSectionByUserId(String userId);



    /**
     * 添加中间关联表
     *
     * @param userId
     * @param sectionId
     */
    @Insert("insert into users_section values(#{userId},#{sectionId})")
    void addUserSection(@Param("userId") String userId, @Param("sectionId") String sectionId);


}
